package org.siqisource.webstart.context;

import javax.servlet.ServletContext;

import org.springframework.web.context.ServletContextAware;

public class Path implements ServletContextAware {

	/** apache反向代理的路径，用于webroot解析 */
	private static String proxyPath;

	/** 应用路径，用于开发应用时使用 */
	private static String appPath;


	/** contextPath -上下文路径 */
	private static String contextPath;

	/** physicalPath -物理路径 */
	private static String physicalPath;

	@Override
	public void setServletContext(ServletContext servletContext) {
		// spring先依据配置文件夹设置了成员属性，然后再执行这个方法
		initPathInfo(servletContext);

		if (proxyPath != null && !"".equals(proxyPath)) {// set contextPath as
															// proxy path
			if ("/".equals(proxyPath)) {
				contextPath = "";
			} else {
				contextPath = proxyPath;
			}
		}

		if (appPath == null || "".equals(appPath)) {
			appPath = contextPath;
		}

	}

	/**
	 * <p>
	 * Title: initPathInfo
	 * </p>
	 * <p>
	 * Description: init static Path;
	 * </p>
	 */
	private void initPathInfo(ServletContext servletContext) {

		// 获得web路径
		String sysContextPath = servletContext.getContextPath();
		// 不同的容器对getContextPath的返回值不同，统一删除最后的斜线
		int iLastSlash = sysContextPath.lastIndexOf('/');
		if (iLastSlash != -1 && iLastSlash == sysContextPath.length() - 1) {
			sysContextPath = sysContextPath.substring(0, iLastSlash);
		}
		contextPath = sysContextPath;

		// 获得物理路径
		String sysPhysicalPath = servletContext.getRealPath("/");
		physicalPath = sysPhysicalPath;

	}

	public static String getProxyPath() {
		return proxyPath;
	}

	public static void setProxyPath(String proxyPath) {
		Path.proxyPath = proxyPath;
	}

	public static String getAppPath() {
		return appPath;
	}

	public static void setAppPath(String appPath) {
		Path.appPath = appPath;
	}

	public static String getContextPath() {
		return contextPath;
	}

	public static void setContextPath(String contextPath) {
		Path.contextPath = contextPath;
	}

	public static String getPhysicalPath() {
		return physicalPath;
	}

	public static void setPhysicalPath(String physicalPath) {
		Path.physicalPath = physicalPath;
	}

	 

}
